package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.common.result.PageResult;
import com.tsing.mapper.NewsMapper;
import com.tsing.pojo.dto.NewsDTO;
import com.tsing.pojo.entity.TlPost;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/4 20:56
 */
public class NewsMapperImpl extends CommonDao implements NewsMapper {
    @Override
    public PageResult<TlPost> list(Integer page, Integer size, Integer status, String title) {
        int offset = (page - 1) * size;
        PageResult<TlPost> result = new PageResult<>();

        String statusCondition = (status != null) ? "AND status = ?" : "";
        String nameCondition = (title != null) ? "AND title like ?" : "";

        String sql = "SELECT * FROM tl_post WHERE 1=1 " + nameCondition + " " + statusCondition + "order by " +
                "postId desc " +
                " LIMIT ?,?";

        List<Object> params = new ArrayList<>();
        if (title != null) {
            params.add("%" + title + "%");
        }
        if (status != null) {
            params.add(status);
        }
        params.add(offset);   // change this line
        params.add(size);     // change this line

        List<TlPost> data = null;
        try {
            data = executeQuery(TlPost.class, sql, params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        result.setData(data);

        try {
            result.setTotal(executeCount(
                    "SELECT count(?) FROM tl_post",
                    new Object[]{1}));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        result.setSize(size);
        result.setCode(200);
        result.setCurrentPage(page);

        return result;
    }

    @Override
    public int update(Integer id, Integer status, String msg) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("UPDATE tl_post SET status = ? ");

        params.add(status);

        if (msg != null) {
            sqlBuilder.append(",msg = ?, ");
            params.add(msg);
        }

        sqlBuilder.append("WHERE postId = ?");
        params.add(id);

        String sql = sqlBuilder.toString();

        int result = 0;

        try {
            result = executeUpdate(sql, params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return result;
    }
    @Override
    public PageResult<TlPost> findPublished(NewsDTO newsDTO) {
        int offset = (newsDTO.getPage() - 1) * newsDTO.getSize();

        Integer page = newsDTO.getPage();
        Integer size = newsDTO.getSize();
        Timestamp beginDate = newsDTO.getBeginDate();
        Timestamp endDate = newsDTO.getEndDate();
        String title = newsDTO.getTitle();
        Integer status = newsDTO.getEnable();

        PageResult<TlPost> result = new PageResult<>();

        StringBuilder sql = new StringBuilder("SELECT * FROM tl_post where 1=1");

        List<Object> paramsCount = new ArrayList<>();

        if (title != null) {
            sql.append(" and title like ?");
            paramsCount.add("%" + title + "%");
        }
        if (status != null) {
            sql.append(" and status = ?");
            paramsCount.add(status);
        }
        if (beginDate != null) {
            sql.append(" and createDate >= ?");
            paramsCount.add(beginDate);
        }
        if (endDate != null) {
            sql.append(" and createDate <= ?");
            paramsCount.add(endDate);
        }

        sql.append(" limit ?,?");
        paramsCount.add(offset);
        paramsCount.add(size);

        System.out.println("Executing query: " + sql);
        System.out.println("With parameters: " + Arrays.toString(paramsCount.toArray()));

        List<TlPost> tlPosts = null;

        try {
            tlPosts = executeQuery(TlPost.class, sql.toString(), paramsCount.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            result.setTotal(executeCount("select count(?) from tl_post", new Object[]{1}));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        result.setData(tlPosts);
        result.setSize(size);
        result.setCode(200);
        result.setCurrentPage(page);

        return result;
    }

    @Override
    public int down_or_up(Integer id, Integer enable) {
        int result = 0;
        try {
            result = executeUpdate("UPDATE tl_post SET status = ? WHERE postId = ?", new Object[]{enable, id});
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public TlPost one(Integer id) {
        String sql = "SELECT * FROM tl_post WHERE postId = ?";
        try {
            return executeSingleQuery(TlPost.class, sql, new Object[]{id});
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
